home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / base / netkit-b.07a / netkit-b / NetKit-B-0.07A / ftp / ruserpass.c < prev   
Encoding:
C/C++ Source or Header  |  1996-07-14  |  7.2 KB  |  283 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. /*
  35.  * from: @(#)ruserpass.c    5.3 (Berkeley) 3/1/91
  36.  */
  37. char ruserpass_rcsid[] = 
  38.   "$Id: ruserpass.c,v 1.2 1996/07/14 09:16:00 dholland Exp $";
  39.  
  40. #include <sys/types.h>
  41. #include <stdio.h>
  42. #include <utmp.h>
  43. #include <ctype.h>
  44. #include <sys/stat.h>
  45. #include <errno.h>
  46. #include <string.h>
  47. #include <unistd.h>
  48. #include "ftp_var.h"
  49.  
  50. char    *renvlook(), *malloc(), *index(), *getenv(), *getpass(), *getlogin();
  51. char    *strcpy();
  52. struct    utmp *getutmp();
  53. static    FILE *cfile;
  54.  
  55. #define    DEFAULT    1
  56. #define    LOGIN    2
  57. #define    PASSWD    3
  58. #define    ACCOUNT 4
  59. #define MACDEF  5
  60. #define    ID    10
  61. #define    MACH    11
  62.  
  63. static char tokval[100];
  64.  
  65. static struct toktab {
  66.     char *tokstr;
  67.     int tval;
  68. } toktab[]= {
  69.     { "default",    DEFAULT },
  70.     { "login",    LOGIN },
  71.     { "password",    PASSWD },
  72.     { "passwd",    PASSWD },
  73.     { "account",    ACCOUNT },
  74.     { "machine",    MACH },
  75.     { "macdef",    MACDEF },
  76.     { NULL,        0 }
  77. };
  78.  
  79. int
  80. xruserpass(const char *host, char **aname, char **apass, char **aacct)
  81. {
  82.     char *hdir, buf[BUFSIZ], *tmp;
  83.     char myname[MAXHOSTNAMELEN], *mydomain;
  84.     int t, i, c, usedefault = 0;
  85.     struct stat stb;
  86.     static int token();
  87.  
  88.     hdir = getenv("HOME");
  89.     if (hdir == NULL)
  90.         hdir = ".";
  91.     (void) sprintf(buf, "%s/.netrc", hdir);
  92.     cfile = fopen(buf, "r");
  93.     if (cfile == NULL) {
  94.         if (errno != ENOENT)
  95.             perror(buf);
  96.         return(0);
  97.     }
  98.     if (gethostname(myname, sizeof(myname)) < 0)
  99.         myname[0] = '\0';
  100.     if ((mydomain = index(myname, '.')) == NULL)
  101.         mydomain = "";
  102. next:
  103.     while ((t = token())) switch(t) {
  104.  
  105.     case DEFAULT:
  106.         usedefault = 1;
  107.         /* FALL THROUGH */
  108.  
  109.     case MACH:
  110.         if (!usedefault) {
  111.             if (token() != ID)
  112.                 continue;
  113.             /*
  114.              * Allow match either for user's input host name
  115.              * or official hostname.  Also allow match of 
  116.              * incompletely-specified host in local domain.
  117.              */
  118.             if (strcasecmp(host, tokval) == 0)
  119.                 goto match;
  120.             if (strcasecmp(hostname, tokval) == 0)
  121.                 goto match;
  122.             if ((tmp = index(hostname, '.')) != NULL &&
  123.                 strcasecmp(tmp, mydomain) == 0 &&
  124.                 strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
  125.                 tokval[tmp - hostname] == '\0')
  126.                 goto match;
  127.             if ((tmp = index(host, '.')) != NULL &&
  128.                 strcasecmp(tmp, mydomain) == 0 &&
  129.                 strncasecmp(host, tokval, tmp - host) == 0 &&
  130.                 tokval[tmp - host] == '\0')
  131.                 goto match;
  132.             continue;
  133.         }
  134.     match:
  135.         while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
  136.  
  137.         case LOGIN:
  138.             if (token())
  139.                 if (*aname == 0) { 
  140.                     *aname = malloc((unsigned) strlen(tokval) + 1);
  141.                     (void) strcpy(*aname, tokval);
  142.                 } else {
  143.                     if (strcmp(*aname, tokval))
  144.                         goto next;
  145.                 }
  146.             break;
  147.         case PASSWD:
  148.             if (strcmp(*aname, "anonymous") &&
  149.                 fstat(fileno(cfile), &stb) >= 0 &&
  150.                 (stb.st_mode & 077) != 0) {
  151.     fprintf(stderr, "Error - .netrc file not correct mode.\n");
  152.     fprintf(stderr, "Remove password or correct mode.\n");
  153.                 goto bad;
  154.             }
  155.             if (token() && *apass == 0) {
  156.                 *apass = malloc((unsigned) strlen(tokval) + 1);
  157.                 (void) strcpy(*apass, tokval);
  158.             }
  159.             break;
  160.         case ACCOUNT:
  161.             if (fstat(fileno(cfile), &stb) >= 0
  162.                 && (stb.st_mode & 077) != 0) {
  163.     fprintf(stderr, "Error - .netrc file not correct mode.\n");
  164.     fprintf(stderr, "Remove account or correct mode.\n");
  165.                 goto bad;
  166.             }
  167.             if (token() && *aacct == 0) {
  168.                 *aacct = malloc((unsigned) strlen(tokval) + 1);
  169.                 (void) strcpy(*aacct, tokval);
  170.             }
  171.             break;
  172.         case MACDEF:
  173.             if (proxy) {
  174.                 (void) fclose(cfile);
  175.                 return(0);
  176.             }
  177.             while ((c=getc(cfile)) != EOF && (c == ' ' || c == '\t'));
  178.             if (c == EOF || c == '\n') {
  179.                 printf("Missing macdef name argument.\n");
  180.                 goto bad;
  181.             }
  182.             if (macnum == 16) {
  183.                 printf("Limit of 16 macros have already been defined\n");
  184.                 goto bad;
  185.             }
  186.             tmp = macros[macnum].mac_name;
  187.             *tmp++ = c;
  188.             for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
  189.                 !isspace(c); ++i) {
  190.                 *tmp++ = c;
  191.             }
  192.             if (c == EOF) {
  193.                 printf("Macro definition missing null line terminator.\n");
  194.                 goto bad;
  195.             }
  196.             *tmp = '\0';
  197.             if (c != '\n') {
  198.                 while ((c=getc(cfile)) != EOF && c != '\n');
  199.             }
  200.             if (c == EOF) {
  201.                 printf("Macro definition missing null line terminator.\n");
  202.                 goto bad;
  203.             }
  204.             if (macnum == 0) {
  205.                 macros[macnum].mac_start = macbuf;
  206.             }
  207.             else {
  208.                 macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
  209.             }
  210.             tmp = macros[macnum].mac_start;
  211.             while (tmp != macbuf + 4096) {
  212.                 if ((c=getc(cfile)) == EOF) {
  213.                 printf("Macro definition missing null line terminator.\n");
  214.                     goto bad;
  215.                 }
  216.                 *tmp = c;
  217.                 if (*tmp == '\n') {
  218.                     if (*(tmp-1) == '\0') {
  219.                        macros[macnum++].mac_end = tmp - 1;
  220.                        break;
  221.                     }
  222.                     *tmp = '\0';
  223.                 }
  224.                 tmp++;
  225.             }
  226.             if (tmp == macbuf + 4096) {
  227.                 printf("4K macro buffer exceeded\n");
  228.                 goto bad;
  229.             }
  230.             break;
  231.         default:
  232.     fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
  233.             break;
  234.         }
  235.         goto done;
  236.     }
  237. done:
  238.     (void) fclose(cfile);
  239.     return(0);
  240. bad:
  241.     (void) fclose(cfile);
  242.     return(-1);
  243. }
  244.  
  245. static int
  246. token()
  247. {
  248.     char *cp;
  249.     int c;
  250.     struct toktab *t;
  251.  
  252.     if (feof(cfile))
  253.         return (0);
  254.     while ((c = getc(cfile)) != EOF &&
  255.         (c == '\n' || c == '\t' || c == ' ' || c == ','))
  256.         continue;
  257.     if (c == EOF)
  258.         return (0);
  259.     cp = tokval;
  260.     if (c == '"') {
  261.         while ((c = getc(cfile)) != EOF && c != '"') {
  262.             if (c == '\\')
  263.                 c = getc(cfile);
  264.             *cp++ = c;
  265.         }
  266.     } else {
  267.         *cp++ = c;
  268.         while ((c = getc(cfile)) != EOF
  269.             && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  270.             if (c == '\\')
  271.                 c = getc(cfile);
  272.             *cp++ = c;
  273.         }
  274.     }
  275.     *cp = 0;
  276.     if (tokval[0] == 0)
  277.         return (0);
  278.     for (t = toktab; t->tokstr; t++)
  279.         if (!strcmp(t->tokstr, tokval))
  280.             return (t->tval);
  281.     return (ID);
  282. }
  283.